package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.user.mapper.UserMapper;
import com.heima.user.pojos.LoginDto;
import com.heima.user.pojos.User;
import com.heima.user.service.UserService;

import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;

import static com.heima.model.common.enums.AppHttpCodeEnum.*;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public ResponseResult login(LoginDto dto) {
     /*   //1.先判断是游客登录还是正常输入账号和密码登录
        if(StringUtils.isBlank(dto.getPhone())||StringUtils.isBlank(dto.getPassword())){
            //游客登录,直接返回固定值的token
            String token = AppJwtUtil.getToken(0L);
            Map<String,Object> map=new HashMap<>();
            map.put("token",token);
            return ResponseResult.okResult(map);
        }//不走上面的话就是正常登录逻辑
        else {
        String phone = dto.getPhone();
        User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, phone));
        if (null==user){//用户名不存在
            return ResponseResult.errorResult(AP_USER_DATA_NOT_EXIST);
        }//不走上面就代表数据库中有这个用户,开始判断密码
        String password = dto.getPassword();
        String salt = user.getSalt();
        String md5pwd = DigestUtils.md5DigestAsHex((password + salt).getBytes(StandardCharsets.UTF_8));
        if (!md5pwd.equals(user.getPassword())){
            return ResponseResult.errorResult(LOGIN_PASSWORD_ERROR);
        }//上面不走代表密码正确,封装返回值给前端一个"user",一个"token"
        Map<String,Object> map=new HashMap<>();
        user.setSalt("");
        user.setPassword("");
        map.put("user",user);
        String token = AppJwtUtil.getToken(user.getId().longValue());
        map.put("tokne",token);
        return ResponseResult.okResult(map);}
    }*/
        //1.正常登录(dto有值)
        if (StringUtils.isNotEmpty(dto.getPhone())){//有值代表前端传了手机号
            User sqluser = getOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, dto.getPhone()));
            if (null!=sqluser){//数据库中查到有这个用户,开始比对密码
                //先拿到盐再加密
                String salt = sqluser.getSalt();
                String md5pwd = DigestUtils.md5DigestAsHex((dto.getPassword() + salt).getBytes(StandardCharsets.UTF_8));
                if (sqluser.getPassword().equals(md5pwd)){//判断密码跟数据库中的密码进行对比
                    HashMap<String, Object> map = new HashMap<>();
                    sqluser.setSalt("");
                    sqluser.setPassword("");
                    map.put("user",sqluser);//放用户信息
                    String token = AppJwtUtil.getToken(sqluser.getId().longValue());
                    map.put("token",token);//设置Token回去
                  return ResponseResult.okResult(map);
                }else {//密码错误
                    return ResponseResult.errorResult(LOGIN_PASSWORD_ERROR);
                }
            }else {
                return ResponseResult.errorResult(AP_USER_DATA_NOT_EXIST);
            }
        }
            //2.游客登录(dto没有值)
            HashMap<String, Object> map = new HashMap<>();
            String token = AppJwtUtil.getToken(0L);
            map.put("token",token);
            return ResponseResult.okResult(map);

    }
}
